﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LockCrypt.Core;

namespace LockCrypt.DAL {
    public class DataCreator {
        /// <summary>
        /// Adds default accounts and templates to a database.
        /// </summary>
        public static void AddDummyData() {
            AddDummyData(true);
        }

        /// <summary>
        /// Adds default accounts and templates to a database.
        /// </summary>
        public static void AddDummyData(bool includeTemplates) {
            ILogProvider logger = new ConsoleLogger();
            var groups = new GroupBase[] {
                new GroupBase() {
                    ID="__samp1",
                    Name = "Accounts",
                    SortIndex = 0
                },
                new GroupBase() {
                    ID="__samp2",
                    Name = "Credit Cards",
                    SortIndex = 0,
                    ParentID = "__samp1"
                },
                new GroupBase() {
                    ID="__samp3",
                    Name = "Bank Accounts",
                    SortIndex = 1,
                    ParentID = "__samp1"
                },
                new GroupBase() {
                    ID="__samp4",
                    Name = "Contacts",
                    SortIndex = 0
                },
                new GroupBase() {
                    ID="__samp5",
                    Name = "Misc",
                    SortIndex = 0
                },
                new GroupBase() {
                    ID="__samp6",
                    Name = "Websites",
                    SortIndex = 0
                }
            };
            try {
                DalConfigurationManager.Provider.AddGroups(groups, logger);
            } catch(Exception ex) {
                logger.Log(string.Format("Error adding default groups: {0}", ex.Message), true);
            }
            //"IÃ±tÃ«rnÃ¢tiÃ´nÃ lizÃ¦tiÃ¸n Support", new String[] {lang.getString("SERIAL"), "flag_blue","Other","Ã„Ã¤Ã¡Ã©Ã–Ã¶Å‘Ã³ÃœÃ¼ÃŸ","Ð±ÑŠÐ»Ð³Ð°Ñ€ÑÐºÐ¸","Ð ÑƒÑÑÐºÐ¸Ð¹", ""});*/
            if(includeTemplates) {
                AddTemplates();
            }
            var templates = DalConfigurationManager.Provider.GetTemplates();
            templates.AddRange(GetTemplates());
            var accounts = new AccountBase[] {
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "BankAccount")), "Natwest Current", "__samp3", "creditcard", new String[] { "Natwest", "London", "01-01-01", "123498765", "1234", "08451 404 404", "08451 404 404", "", "http://www.natwest.com" }),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "Contact")), "Harold Zoid", "__samp4", "user_office", new String[] { "Harold", "Zoid", "54", "Las Vegas Blvd.", "", "", "New New York", "New York", "NNY 40939", "USA", "h.zoid@lobstertainment.com", "1-555-3227" }),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "CombinationLock")), "Garage Lock", "__samp5", "padlock",new String[] {"Parking lot B", "55678"}),
                CreateAccount(templates.First(t=>t.Name == "Maestro"), "First Direct Maestro", "__samp1", "creditcard",new String[] {"675999 999999999999", "An", "Other", "1", "02/2010", "4567", "771", "0800 123 456", "http://www.bankofchocolate.com"}),
                CreateAccount(templates.First(t=>t.Name == "MasterCard"), "MBNA MasterCard", "__samp2","creditcard",new String[] {"675999 999999999999", "An", "Other", "1", "01/2008", "02/2010", "4567", "771", "0800 123 456", "http://www.creditcardsrus.com"}, "Assistant's name was danielle. Ask her for discount."),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "MembershipCard")), "Gym Membership Card", "__samp5", "home",new String[] {"Fitness First", "FF13579246", "01/2010", "01632 772 767"}, "Use the Prince Street entrance after 6:30"),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "Prescription")), "Headache Tablets","__samp5", "medicine" ,new String[] {"Sodium Benzoate","3 tablets","Twice daily", "Acme", "Dr Scratchansniff", "Acme Pharmacy", "555-3227"}),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "SerialNumber")), "Windows Serial", "__samp5", "pirate",new String[] {"Other","Microsoft","FCKGW-N0S3R-1ALS0-RRYM4-D3UP0", "02/2006", "support@m6.com"}),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "Website")), "Neowin Login", "__samp6", "network",new String[] {"http://www.neowin.net/","guest","notapassword", "", "someone@gmail.com"}),
                CreateAccount(templates.First(t=>t.Name == I18nUtils.GetString("Strings", "MembershipCard")), "Iñtërnâtiônàlizætiøn Support", "__samp6", "flag_blue",new String[] {"http://www.neowin.net/","ÄäáéÖöőóÜüß","български","Русский"}, "Iñtërnâtiônàlizætiøn<>/\\&@#\"',Русский,使用偏好,プライバシー,ციუმიնալիនិយកម្ម​ស្រៈអោតូបនិយកतिविधि ईगतिविيَّارَة جَيِّدَة طور بين خการทําใ\n\nLockCrypt Supports: Western,Cyrillic,Chinese,Japanese,Georgian,Armenian,Khmer 1,Khmer 2,Hindi 1,Hindi 2,Arabic 1,Arabic 2,Urdu,(Thai,Tibetan,Myanmar) and probably a few others")
            };
            try {
                DalConfigurationManager.Provider.AddAccounts(accounts, logger);
            } catch(Exception ex) {
                logger.Log(string.Format("Error adding default accounts: {0}", ex.Message), true);
            }
        }

        /// <summary>
        /// Creates an account.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="name">The name.</param>
        /// <param name="groupId">The group ID.</param>
        /// <param name="icon">The icon.</param>
        /// <param name="fieldValues">The field values.</param>
        /// <returns>An account with the specified data</returns>
        private static AccountBase CreateAccount(ITemplate template, string name, string groupId, string icon, string[] fieldValues) {
            return CreateAccount(template, name, groupId, icon, fieldValues, string.Empty);
        }

        /// <summary>
        /// Creates an account.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="name">The name.</param>
        /// <param name="groupId">The group ID.</param>
        /// <param name="icon">The icon.</param>
        /// <param name="fieldValues">The field values.</param>
        /// <param name="notes">The notes.</param>
        /// <returns>An account with the specified data</returns>
        private static AccountBase CreateAccount(ITemplate template, string name, string groupId, string icon, string[] fieldValues, string notes) {
            AccountBase acc = new AccountBase() {
                Name = name,
                Icon = icon,
                ParentID = groupId,
                Template = template,
                Notes = notes
            };
            IField[] fields = template.Fields.OrderBy(f=>f.SortIndex).ToArray();
            int numFields = fields.Length,
                numValues = fieldValues.Length;
            for(int i = 0; i < numFields && i < numValues; i++) {
                IField field = fields[i];
                acc.Fields.Add(new FieldBase() {
                    Name = field.Name,
                    SortIndex = i,
                    Meta = field.Meta,
                    Value = fieldValues[i]
                });
            }
            return acc;
        }

        /// <summary>
        /// Adds the default account templates.
        /// </summary>
        public static void AddTemplates() {
            ILogProvider logger = new ConsoleLogger();
            var templates = GetTemplates();
            try {
                DalConfigurationManager.Provider.AddTemplates(templates, logger);
            } catch(Exception ex) {
                logger.Log(string.Format("Error adding default templates: {0}", ex.Message), true);
            }
        }

        /// <summary>
        /// Gets the default templates.
        /// </summary>
        /// <returns>All default account templates.</returns>
        private static IEnumerable<ITemplate> GetTemplates() {
            var templates = new TemplateBase[] {
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "Blank"),
                    ShowNotes = true,
                    Fields = new List<IField>()
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "Contact"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="First Name" }},
                        {new FieldBase() { SortIndex=1, Name="Last Name"}},
                        {new FieldBase() { SortIndex=2, Name="House Number"}},
                        {new FieldBase() { SortIndex=3, Name="Line 1"}},
                        {new FieldBase() { SortIndex=4, Name="Line 2"}},
                        {new FieldBase() { SortIndex=5, Name="Line 3"}},
                        {new FieldBase() { SortIndex=6, Name="City"}},
                        {new FieldBase() { SortIndex=7, Name="State/Province"}},
                        {new FieldBase() { SortIndex=8, Name="Post Code/ZIP"}}, 
                        {new FieldBase() { SortIndex=9, Name="Country"}}, 
                        {new FieldBase() { SortIndex=10, Name="Email Address"}}, 
                        {new FieldBase() { SortIndex=11, Name="Phone", Meta= FieldMetaData.Phone}}
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "BankAccount"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Bank" }},
                        {new FieldBase() { SortIndex=1, Name="Branch" }},
                        {new FieldBase() { SortIndex=2, Name="Sort Code" }},
                        {new FieldBase() { SortIndex=3, Name="Account Number" }},
                        {new FieldBase() { SortIndex=4, Name="PIN", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=5, Name="If Lost", Meta= FieldMetaData.Phone }},
                        {new FieldBase() { SortIndex=6, Name="Phone", Meta= FieldMetaData.Phone }},
                        {new FieldBase() { SortIndex=7, Name="IBAN" }},
                        {new FieldBase() { SortIndex=8, Name="Online Banking", Meta= FieldMetaData.Url }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "CombinationLock"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Location" }},
                        {new FieldBase() { SortIndex=1, Name="Code", Meta= FieldMetaData.Password }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "CreditCard"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Card Number" }},
                        {new FieldBase() { SortIndex=1, Name="First Name" }},
                        {new FieldBase() { SortIndex=2, Name="Last Name" }},
                        {new FieldBase() { SortIndex=3, Name="Issue" }},
                        {new FieldBase() { SortIndex=4, Name="Start Date" }},
                        {new FieldBase() { SortIndex=5, Name="Expiry Date" }},
                        {new FieldBase() { SortIndex=6, Name="PIN", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=7, Name="CCV", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=8, Name="If Lost", Meta= FieldMetaData.Phone }},
                        {new FieldBase() { SortIndex=9, Name="Online Banking", Meta= FieldMetaData.Url }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "DrivingLicense"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Name" }},
                        {new FieldBase() { SortIndex=1, Name="Valid from" }},
                        {new FieldBase() { SortIndex=2, Name="Valid to" }},
                        {new FieldBase() { SortIndex=3, Name="Issuing Authority" }},
                        {new FieldBase() { SortIndex=4, Name="License Number" }},
                        {new FieldBase() { SortIndex=5, Name="Category" }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "MembershipCard"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Company" }},
                        {new FieldBase() { SortIndex=1, Name="Membership number" }},
                        {new FieldBase() { SortIndex=2, Name="Expires" }},
                        {new FieldBase() { SortIndex=3, Name="Phone", Meta= FieldMetaData.Phone }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "EmailAccount"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Login" }},
                        {new FieldBase() { SortIndex=1, Name="Password", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=2, Name="POP3" }},
                        {new FieldBase() { SortIndex=3, Name="SMTP" }},
                        {new FieldBase() { SortIndex=4, Name="IMAP" }},
                        {new FieldBase() { SortIndex=5, Name="Webmail", Meta= FieldMetaData.Url }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "Prescription"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Drug" }},
                        {new FieldBase() { SortIndex=1, Name="Dosage" }},
                        {new FieldBase() { SortIndex=2, Name="Frequency" }},
                        {new FieldBase() { SortIndex=3, Name="Brand" }},
                        {new FieldBase() { SortIndex=4, Name="Doctor" }},
                        {new FieldBase() { SortIndex=5, Name="Pharmacy" }},
                        {new FieldBase() { SortIndex=6, Name="Phone", Meta= FieldMetaData.Phone }},
                    }
                },
                new TemplateBase() {
                    Name = "MasterCard",
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Card Number" }},
                        {new FieldBase() { SortIndex=1, Name="First Name" }},
                        {new FieldBase() { SortIndex=2, Name="Last Name" }},
                        {new FieldBase() { SortIndex=3, Name="Issue" }},
                        {new FieldBase() { SortIndex=4, Name="Start Date" }},
                        {new FieldBase() { SortIndex=5, Name="Expiry Date" }},
                        {new FieldBase() { SortIndex=6, Name="PIN", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=7, Name="CCV", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=8, Name="If Lost", Meta= FieldMetaData.Phone }},
                        {new FieldBase() { SortIndex=9, Name="Online Banking", Meta= FieldMetaData.Url }},
                    }
                },
                new TemplateBase() {
                    Name = "Maestro",
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Card Number" }},
                        {new FieldBase() { SortIndex=1, Name="First Name" }},
                        {new FieldBase() { SortIndex=2, Name="Last Name" }},
                        {new FieldBase() { SortIndex=3, Name="Issue" }},
                        {new FieldBase() { SortIndex=4, Name="Expiry Date" }},
                        {new FieldBase() { SortIndex=5, Name="PIN", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=6, Name="CCV", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=7, Name="If Lost", Meta= FieldMetaData.Phone }},
                        {new FieldBase() { SortIndex=8, Name="Online Banking", Meta= FieldMetaData.Url }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "SerialNumber"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Company" }},
                        {new FieldBase() { SortIndex=1, Name="Serial Number" }},
                        {new FieldBase() { SortIndex=2, Name="Purchased" }},
                        {new FieldBase() { SortIndex=3, Name="Email" }},
                    }
                },
                new TemplateBase() {
                    Name = "Visa",
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="Card Number" }},
                        {new FieldBase() { SortIndex=1, Name="First Name" }},
                        {new FieldBase() { SortIndex=2, Name="Last Name" }},
                        {new FieldBase() { SortIndex=3, Name="Issue" }},
                        {new FieldBase() { SortIndex=4, Name="Start Date" }},
                        {new FieldBase() { SortIndex=5, Name="Expiry Date" }},
                        {new FieldBase() { SortIndex=6, Name="PIN", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=7, Name="If Lost", Meta= FieldMetaData.Phone }},
                        {new FieldBase() { SortIndex=8, Name="Online Banking", Meta= FieldMetaData.Url }},
                    }
                },
                new TemplateBase() {
                    Name = I18nUtils.GetString("Strings", "Website"),
                    ShowNotes = true,
                    Fields = new List<IField>() {
                        {new FieldBase() { SortIndex=0, Name="URL", Meta = FieldMetaData.Url }},
                        {new FieldBase() { SortIndex=1, Name="UserName" }},
                        {new FieldBase() { SortIndex=2, Name="Password", Meta= FieldMetaData.Password }},
                        {new FieldBase() { SortIndex=3, Name="Registered Email" }},
                    }
                }
            };
            return templates;
        }
    }
}
